//
//  ZZCTRRefundController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCTRRefundController.h"
#import "ZZCTRUbiCell.h"
#import "ZZCTRRefund.h"

static NSString *TRRefundCell = @"TRRefundCell";
@interface ZZCTRRefundController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *TRRefundTableView;
@property (strong, nonatomic) ZZCTRUbiCell *cell;
@property (strong, nonatomic) NSMutableArray *trrefundData;
@property (copy,   nonatomic) NSString *tradeID;
@end

@implementation ZZCTRRefundController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ZZCBGColor;
    [self requestData];
    [self.view addSubview:self.TRRefundTableView];
    
}

- (void)requestData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"page":@(1)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"page":@(1)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDITRECORD parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**%@",responseObject);
            self.trrefundData = [ZZCTRRefund mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [self.TRRefundTableView reloadData];
        }
    }];
}

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 15;
    return _trrefundData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    _cell = [tableView dequeueReusableCellWithIdentifier:TRRefundCell];
    _cell.selectionStyle = 0;
    _cell.TrrefundModel = self.trrefundData[indexPath.row];
    _cell.tag = [_cell.TrrefundModel.id integerValue];
    
    //添加长按手势
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    
    longPressGesture.minimumPressDuration=1.5f;//设置长按 时间
    [_cell addGestureRecognizer:longPressGesture];

    return _cell;
}
//长按手势响应编辑模式
- (void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer {
    [self.TRRefundTableView setEditing:YES animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//        [tableView setEditing:YES animated:YES];
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZCLog(@"删除了第%ld",(long)indexPath.row);
//    _tradeID = ;
    
        [self deleteDataAction];
    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
        {
            //修改数据源，在刷新 tableView
            [_trrefundData removeObjectAtIndex:indexPath.row];
            
            //让表视图删除对应的行
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView setEditing:NO animated:YES];
        }
            break;
//        case UITableViewCellEditingStyleInsert:
//        {
//            [_dataSource insertObject:@"我是新增" atIndex:indexPath.row];
//            //让表视图添加对应的行
//            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        }
//            break;
            
        default:
            break;
    }
}
- (void)deleteDataAction {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token,@"id":@"0"};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token,@"id":@"0"};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDITRECORDDELETE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-**00000%@",responseObject);
        }
    }];

}
#pragma mark - 懒加载
- (UITableView *)TRRefundTableView {
    if (_TRRefundTableView == nil) {
        _TRRefundTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ZZCScreenW, ZZCScreenH-108)];
        [_TRRefundTableView registerNib:[UINib nibWithNibName:@"ZZCTRUbiCell" bundle:nil] forCellReuseIdentifier:TRRefundCell];
        _TRRefundTableView.delegate = self;
        _TRRefundTableView.dataSource = self;
        _TRRefundTableView.separatorStyle = NO;
        _TRRefundTableView.showsVerticalScrollIndicator = NO;
        _TRRefundTableView.backgroundColor = ZZCBGColor;
    }
    return _TRRefundTableView;
}
- (NSMutableArray *)trrefundData {
    if (_trrefundData == nil) {
        _trrefundData = [NSMutableArray array];
    }
    return _trrefundData;
}

@end
